/*package cn.jy.nvr.service.impl;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jy.nvr.datasource.DataSourceConst;
import cn.jy.nvr.datasource.DataSourceContextHolder;
import cn.jy.nvr.domain.Channel;
import cn.jy.nvr.domain.Donor;
import cn.jy.nvr.domain.DonorFileName;
import cn.jy.nvr.domain.DonorInfo;
import cn.jy.nvr.domain.DownloadItem;
import cn.jy.nvr.downfile.DownFile;
import cn.jy.nvr.mapper.ChannelMapper;
import cn.jy.nvr.mapper.DonorFileNameMapper;
import cn.jy.nvr.mapper.DonorInfoMapper;
import cn.jy.nvr.mapper.DonorMapper;
import cn.jy.nvr.service.DownloadfileQuartzJob;

@Service("quartzJob")
public class DownloadQuartzJobImpl implements DownloadfileQuartzJob{
	
	//static{DownFile.getInstance().initAndLogin();}
	@Autowired
	DonorMapper donorMapper;
	@Autowired
	ChannelMapper channelMapper;
	@Autowired
	DonorFileNameMapper donorFileNameMapper;
	@Autowired
	DonorInfoMapper donorInfoMapper;
	Logger logger=Logger.getLogger(DownloadQuartzJobImpl.class);
	public void work() {
		//DataSourceContextHolder.setDataSourceType(DataSourceConst.MACHINE);  
		System.out.println("---开始下载任务---");
		 DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		List<Donor> all = donorMapper.getTask();
		if (!all.isEmpty()) {
			for (Donor donor : all) {
				String time=donor.getEND_TIME();
				System.out.println("结束时间" + time);
		        System.out.println(donor.getDONOR_ID());
		        if ((time == null) || (time.length() < 0)) {
		          continue;
		        }
				Date timeDate=null;
				try {
					 timeDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
				} catch (ParseException e1) {
					e1.printStackTrace();
				}
				 DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				donorMapper.UpdateDownloadTask(String.valueOf(donor.getID()));
				DonorFileName donorFileName=new DonorFileName();
				donorFileName.setDonor_id(donor.getDONOR_ID());
				donorFileName.setStarttime(donor.getSTART_TIME());
				donorFileName.setEndtime(donor.getEND_TIME());
				//根据机器号找到对应的通道号和通道号类型
				 DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				 Channel channeliInfo = channelMapper.getChannelByMACHINELOGID(donor.getMACHINELOGID().substring(0, 3));
				 if(channeliInfo==null){
					 continue;
				 }
				 int year = 0;
				 int month = 0;
				 int day = 0;
				 try {
					Date date= new SimpleDateFormat("yyyy-MM-dd").parse(donor.getSTART_TIME().substring(0, 10));
					Calendar filetime=Calendar.getInstance();
					filetime.setTime(date);
					 year=filetime.get(Calendar.YEAR);
					 month=(filetime.get(Calendar.MONTH) + 1);
					 day=filetime.get(Calendar.DAY_OF_MONTH);
				} catch (ParseException e) {
					e.printStackTrace();
				}
				 String calendarPath=year+"/"+month+"/"+day+"/";
				 File file=new File("D:/apache-tomcat-7.0.79/webapps/Nvr/video/"+calendarPath);
				 if(!file.exists()){
					 file.mkdirs();
				 }
				 DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				DonorInfo cardIdById = donorInfoMapper.getCardIdById(donor.getDONOR_ID().toString());
				if(cardIdById==null){
					continue;
				}
				 String cardId=cardIdById.getCARDID();
				 String fileName=year+""+month+""+day+""+cardId;
			     //根据安装耗材时间下载耗材视频
				 String  installStartTime=donor.getINSTALLSTARTTIME();
				 String  installEndTime=donor.getINSTALLENDTIME();
				 logger.info("安装耗材时间"+installStartTime+"---"+installEndTime);
				 if((installStartTime!=null&&installStartTime.length()>0)&&(installEndTime!=null&&installEndTime.length()>0)){
					 Date installStartTimeDate=null;
					 Date installEndTimeDate=null;
					try {
						 installStartTimeDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(installStartTime);
						 installEndTimeDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(installEndTime);
					} catch (ParseException e) {
						e.printStackTrace();
					}
//					installStartTimeDate=new Date(installStartTimeDate.getTime()-30*1000);
//					installStartTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(installStartTimeDate);
					 if((installEndTimeDate.getTime()-installStartTimeDate.getTime())>3*60*1000){
						 Date temp=new Date(installEndTimeDate.getTime()-2*60*1000);
						 installStartTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(temp);
					 }
					 System.out.println("安装"+installStartTime);
					 DownloadItem downloadItem3 = new DownloadItem();
					 int channel3=Integer.valueOf(channeliInfo.getChannel1());
					 downloadItem3.setChannel(channel3);
					 downloadItem3.setStart(installStartTime);
					 downloadItem3.setEnd(installEndTime);
					 downloadItem3.setFilename(fileName+"i");
					 donorFileName.setFilename2("/"+calendarPath+downloadItem3.getFilename()+"jy.mp4");
					 downloadItem3.setFilepath(file.getAbsolutePath()+"\\");
					 DownFile.getInstance().downFileByTime(downloadItem3);
				 }
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				}
				//根据穿刺时间下载穿刺视频
				 String puctureStartTime=donor.getPUNCTURESTARTTIME();
				 String  puctureEndTime=donor.getPUNCTUREENDTIME();
				 logger.info("穿刺时间"+puctureStartTime+"---"+puctureEndTime);
				 if((puctureStartTime!=null&&puctureStartTime.length()>0)&&(puctureEndTime!=null&&puctureEndTime.length()>0)){
					 Date puctureStartTimeDate=null;
					 Date puctureEndTimeDate=null;
					try {
						puctureStartTimeDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(puctureStartTime);
						puctureEndTimeDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(puctureEndTime);
					} catch (ParseException e) {
						e.printStackTrace();
					}
//					puctureStartTimeDate=new Date(puctureStartTimeDate.getTime()-30*1000);
//					puctureStartTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(puctureStartTimeDate);
					 if((puctureEndTimeDate.getTime()-puctureStartTimeDate.getTime())>3*60*1000){
						 Date temp=new Date(puctureEndTimeDate.getTime()-2*60*1000);
						 puctureStartTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(temp);
					 }
					 DownloadItem downloadItem4 = new DownloadItem();
					 int channel4=Integer.valueOf(channeliInfo.getChannel1());
					 downloadItem4.setChannel(channel4);
					 downloadItem4.setStart(puctureStartTime);
					 downloadItem4.setEnd(puctureEndTime);
					 downloadItem4.setFilename(fileName+"p");
					 donorFileName.setFilename3("/"+calendarPath+downloadItem4.getFilename()+"jy.mp4");
					 downloadItem4.setFilepath(file.getAbsolutePath()+"\\");
					 DownFile.getInstance().downFileByTime(downloadItem4);	
				 }
				 try {
						Thread.sleep(1000);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
				//根据卸载耗材时间下载视频
				 String uninstallStartTime=donor.getUNINSTALLSTARTTIME();
				 String uninstallEndTime=donor.getUNINSTALLENDTIME();
				 logger.info("卸载耗材时间"+uninstallStartTime+"---"+uninstallEndTime);
				 if((uninstallStartTime!=null&&uninstallStartTime.length()>0)&&(uninstallEndTime!=null&&uninstallEndTime.length()>0)){
					 Date uninstallStartTimeDate=null;
					 Date uninstallEndTimeDate=null;
					try {
						uninstallStartTimeDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(uninstallStartTime);
						uninstallEndTimeDate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(uninstallEndTime);
					} catch (ParseException e) {
						e.printStackTrace();
					}
//					uninstallStartTimeDate=new Date(uninstallStartTimeDate.getTime()-30*1000);
//					uninstallStartTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(uninstallStartTimeDate);
					 if((uninstallEndTimeDate.getTime()-uninstallStartTimeDate.getTime())>3*60*1000){
						 Date temp=new Date(uninstallEndTimeDate.getTime()-2*60*1000);
						 uninstallStartTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(temp);
					 }
					 DownloadItem downloadItem5 = new DownloadItem();
					 int channel5=Integer.valueOf(channeliInfo.getChannel1());
					 downloadItem5.setChannel(channel5);
					 downloadItem5.setStart(uninstallStartTime);
					 downloadItem5.setEnd(uninstallEndTime);
					 downloadItem5.setFilename(fileName+"u");
					 donorFileName.setFilename4("/"+calendarPath+downloadItem5.getFilename()+"jy.mp4");
					 downloadItem5.setFilepath(file.getAbsolutePath()+"\\");
					 DownFile.getInstance().downFileByTime(downloadItem5);
				 }
				logger.info("---开始保存日志---"+cardId);
				 DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				donorFileNameMapper.save(donorFileName);
				logger.info("---已经保存日志---");
			}
			
		}
	}

}
*/